greedy strings *1700

Please click on ads to support us..

C++ Code:

/*إِنَّ اللَّهَ وَمَلَائِكَتَهُ يُصَلُّونَ عَلَى النَّبِيِّ ۚ يَا أَيُّهَا الَّذِينَ آمَنُوا صَلُّوا عَلَيْهِ وَسَلِّمُوا تَسْلِيمًا*/
#include <bits/stdc++.h>
using namespace std;
#define IO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define W int g; cin >> g; while (g--)
#define sz(v) int(v.size())
#define FIO freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
typedef long long ll;
const long long N = 3e6 + 7, Mod = 1e9 + 7, INF = 2e18;
ll gcd(ll a, ll b) { return a ? gcd(b % a, a) : b; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll inv(ll a, ll b = Mod) { return 1 < a ? b - inv(b % a, a) * b / a : 1; }
const int dx[9] = { 0, 0, 1, -1, 1, 1, -1, -1, 0 };
const int dy[9] = { 1, -1, 0, 0, -1, 1, 1, -1, 0 };
bool check(string s){
    int i = 0 , j = sz(s) - 1 ;
    while (i<j){
        if(s[i++]==s[j--])
            continue;
        return  false;
    }
    return  true;
}
void solve() {
    int n,ok=0;
    cin>>n;
   map<string,bool>mp,mp2,mp3;
   vector<string>v(n);
   string s;
   for(int i=0;i<n;i++)
       cin>>v[i];
   for(int i=0;i<n;i++){
       if(sz(v[i])==1 || check(v[i])){
           ok = 1;
           break;
       }
       s = v[i];
       std::reverse(s.begin(), s.end());
       if(sz(v[i])==2){
           if(mp2[s] || mp[s])
               ok= 1;
           mp2[v[i]] = 1;
       }
       if(sz(v[i])==3){
            if(mp3[s])
                ok = 1;
            s.pop_back();
            if(mp2[s])
                ok = 1;

            mp3[v[i]]=1;
            mp[v[i].substr(0,2)] = 1;
       }

       if(ok)
           break;
   }
   if(ok)
       cout<<"YES\n";
   else
       cout<<"NO\n";
}
int main() {
    IO;
    W
    solve();
}


Comments

Submit
0 Comments
More Questions

21. Merge Two Sorted Lists
203. Remove Linked List Elements
733. Flood Fill
206. Reverse Linked List
83. Remove Duplicates from Sorted List
116. Populating Next Right Pointers in Each Node
145. Binary Tree Postorder Traversal
94. Binary Tree Inorder Traversal
101. Symmetric Tree
77. Combinations
46. Permutations
226. Invert Binary Tree
112. Path Sum
1556A - A Variety of Operations
136. Single Number
169. Majority Element
119. Pascal's Triangle II
409. Longest Palindrome
1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections
1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation
MAKEPAL Weird Palindrome Making
HILLSEQ Hill Sequence
MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings